package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.converter.VocCategoryConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import com.cskaoyan.wordmemorize.service.BookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 顾海龙
 * @version 1.0
 */
@RestController
@ResponseBody
public class BookCategoryController {
    @Autowired
    BookCategoryService bookCategoryService;

    @Autowired
    VocCategoryConverter vocCategoryConverter;

    //获取指定书籍类目
    @GetMapping("/admin/voc/category")
    public Result getCategoryS(@RequestParam String categoryId){
        VocCategoryDTO category = null;
        try {
            category = bookCategoryService.getCategory(categoryId);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(category);
    }

    //添加书籍类名
    @PostMapping("/admin/voc/category")
    @SaAdminCheckPermission(SystemPermissions.Category.ADD)
    public Result addCategory(@RequestBody VocCategoryCommand vocCategoryCommand){
        try {
            VocCategoryDO vocCategoryDO = vocCategoryConverter.bookCategoryCommand2Entity(vocCategoryCommand);
            bookCategoryService.addCategory(vocCategoryDO);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //修改书籍类目
    @PutMapping("/admin/voc/category")
    @SaAdminCheckPermission(SystemPermissions.Category.UPDATE)
    public Result updateCategory(@RequestBody VocCategoryCommand vocCategoryCommand){
        try {
            VocCategoryDO vocCategoryDO = vocCategoryConverter.bookCategoryCommand2Entity(vocCategoryCommand);
            bookCategoryService.updateCategory(vocCategoryDO);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //分页查询书籍类目
    @GetMapping("/admin/voc/category/list")
    @SaAdminCheckPermission(SystemPermissions.Category.SEARCH)
    public Result selectCategoryList(@RequestParam(required = false) Integer pageNum,@RequestParam(required = false) Integer count,@RequestParam(required = false) String keyword){
        PageDTO<VocCategoryDTO> categoryList = null;
        try {
            PageRequest pageRequest = new PageRequest();
            pageRequest.setPageNum(pageNum);
            pageRequest.setCount(count);
            pageRequest.setKeyword(keyword);
            categoryList = bookCategoryService.getCategoryList(pageRequest);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(categoryList);
    }

    //删除指定书籍类目
    @DeleteMapping("/admin/voc/category/{categoryId}")
    @SaAdminCheckPermission(SystemPermissions.Category.DELETE)
    public Result deleteCategory(@PathVariable String categoryId){
        try {
            bookCategoryService.deleteCategory(categoryId);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //批量删除书籍类目
    @DeleteMapping("/admin/voc/categories")
    @SaAdminCheckPermission(SystemPermissions.Category.BATCH_DELETE)
    public Result deleteCategoryByList(@RequestBody BatchDeleteCommand batchDeleteCommand){
        try {
            bookCategoryService.deleteCategoryByList(batchDeleteCommand.getIds());
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

}
